/**
 * @file 工单操作-派单
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.define(['layer', 'form', 'element', 'table', 'zmnUtils', 'select2', '$http', '$api'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var element = layui.element;

    var zmnUtils = layui.zmnUtils;
    var opCommonUtils = layui.list_modify_common;
    var $http = layui.$http;

    //正常派单列表
    var normalMasterList = json.recommendMasterList;
    //消耗派单列表
    var consumeMasterList = json.masterList;

    var templeteFormatter = {
        markingTempleteFunc: function (d) {
            return '<div><span class="layui-badge layui-bg-blue">' + d + '</span></div>';
        },

        markingAbilityTempleteFunc: function (d) {
            var str = '<div>';
            if (d.abilityList instanceof Array) {
                if (d.abilityList.length > 0) {
                    d.abilityList.forEach(vt => {
                        if (vt.effective == 2 ) {
                            str += '<span style="margin-right:10px;" class="layui-badge layui-bg-blue">' + vt.serviceName + '</span>';
                        } else{
                            str += '<span style="margin-right:10px;" class="layui-badge layui-bg-gray">' + vt.serviceName + '</span>';
                        }
                    });
                }
            }
            return str + '</div>';
        },

        masterInfoTempleteFunc: function (d) {
            return '<div><span class="master" id="' + d.masterId + '"></span></div>';
        },

        masterNameTempleteFunc: function (d) {
            if (!d) {
                return '';
            }
            var text = d.masterName + '—' + zmnUtils.maskPhone(d.masterPhone);
            var isOffline = d.onlineStatus !== 2;
            return (`
               <div>
                   <div>
                        <span>${text}</span>
                        ${d.distributeRuleTitle ? `<span class="layui-badge layui-bg-green">${d.distributeRuleTitle}</span>` : ''}
                        ${isOffline ? '<span class="layui-badge layui-bg-gray">离线</span>' : ''}
                    </div>
                </div>
             `);
        }
    };

    var createTableCols = (function () {
        var cols;
        var normalColsData = [
            {type: 'radio', fixed: 'left'},
            {field: 'masterName', title: '工程师', sort: true, fixed: 'left', minWidth: 290, templet: templeteFormatter.masterNameTempleteFunc},
            {field: 'abilityList', title: '天猫技能标示', sort: true, align: 'left', minWidth: 300, templet:templeteFormatter.markingAbilityTempleteFunc},
            {field: 'vip', title: 'VIP标示', sort: true, align: 'center',  templet: function (data) {return data.vip == 2 ? templeteFormatter.markingTempleteFunc('VIP') : '';}},
            {field: 'bigPromotion', title: '大促标示', sort: true, align: 'center', templet: function (data) {return data.bigPromotion == 2 ? templeteFormatter.markingTempleteFunc('大促') : '';}},
            {field: 'blackList', title: '黑名单', sort: true, align: 'center', templet: function (data) {return data.blackList == 2 ? templeteFormatter.markingTempleteFunc('黑名单') : '';}},
            {field: 'degradation', title: '飞单', sort: true, align: 'center', templet: function (data) {return data.degradation == 2 ? templeteFormatter.markingTempleteFunc('飞单') : '';}},
            {field: 'masterId', title: '占位列', hide: true, templet: templeteFormatter.masterInfoTempleteFunc}
        ]
        cols = [normalColsData];
        return function () {
            return cols;
        }
    })(json);


    function renderDetailMasterList(data) {
        table.render({
            elem: '#table-matser',
            height: 400,
            limit: 1000,
            data: data,
            skin: 'line',
            cols: createTableCols()
        });
    }


    function initTableViewOfMasterList(data) {
        renderDetailMasterList(data);
    }

    function updateMasterSelect(masterId) {
        form.val('zmn-work-form', {masterId: masterId});
    }

    /**
     * 更新检查VIP或大促标示校验原因
     * @param {boolean} show 是否显示
     */
    function updateTypeCheck (show) {
        var $tmallBtn = $('#tmall-btn-show');
        if (show) {
            $tmallBtn.removeClass( 'layui-hide');
        } else {
            $tmallBtn.addClass('layui-hide');
        }
    }


    /**
     * 初始化页面
     */
    function initPage() {
        initTableViewOfMasterList(json.recommendMasterList);
    }

    /**
     * 初始化页面事件
     */
    function initEvent() {
        //监听Tab切换，以改变地址hash值
        element.on('tab(docDemoTabBrief)', function (data) {
            var $this = $(this);
            var data = $this.data('type');
            if (data) {
                data = normalMasterList;
                updateTypeCheck(false);
            } else {
                data = consumeMasterList;
                updateTypeCheck(true);
            }

            updateMasterSelect(null);
            initTableViewOfMasterList(JSON.parse(JSON.stringify(data)));
        });

        table.on('radio(tableOfMatserList)', function (obj) {
            var masterId = obj.data.masterId;
            updateMasterSelect(masterId);
        });


        form.on('submit(distribute)', function (data) {
            if (data.field.masterId === '') {
                layer.msg('请选择工程师');
                return false;
            }

            var $tmallBtn = $('#tmall-btn-show').hasClass('layui-hide');
            if (!$tmallBtn) {
                if (data.field.stopOrderTypeCheckReason === '') {
                    layer.msg('请选择不检查订单类型的原因');
                    return false;
                }
            } else {
                delete data.field.stopOrderTypeCheckReason;
            }

            $http.lockSave(data.form.action, data.field, data.elem).then(function () {
                layer.msg('派单保存成功', {icon: 1, time: 1000}, function () {
                    opCommonUtils.goOperateHome();
                });
            }, function (msg) {
                layer.msg(msg);
            });


            return false;
        });
    }




    // 程序入口
    function init() {
        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();
    }

    // 初始化
    init();

    exports('oms_work_modify_special_tmall_distribute');
});
